--参数1　优惠卷id
local voucherId = ARGV[1]
--参数2 用户id
local userId = ARGV[2]

local stockKey = "seckill:stock:" .. voucherId
local orderKey = "seckill:order:" .. voucherId

-- 判断库存是否充足
local stock = tonumber(redis.call("get", stockKey)) or 0
if (stock <= 0) then
    --库存不足
    return 1
end
-- 判断用户是否已经下过单
if (tonumber(redis.call("sismember", orderKey, userId)) == 1) then
    --用户已经下过单
    return 2
end
-- 扣减库存
redis.call("incrby", stockKey, -1)
redis.call("sadd", orderKey, userId)
return 0






